/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.shrikanthravi.customnavigationdrawer.fraction;

import com.shrikanthravi.customnavigationdrawer.ResourceTable;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;

/**
 * NewsFraction
 *
 * @author wangyin
 * @since 2021/06/23
 */
public class NewsFraction extends Fraction {
    /**
     * 标题
     */
    private String mTitle;

    /**
     * 颜色
     */
    private int color;

    /**
     * 获取实例
     *
     * @param title 标题
     * @param color 颜色
     * @return NewsFraction
     */
    public static NewsFraction getInstance(String title, int color) {
        NewsFraction sf = new NewsFraction();
        sf.mTitle = title;
        sf.color = color;
        return sf;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_fr_news_main, container, false);
        Component textComponent = component.findComponentById(ResourceTable.Id_card_title_tv);
        if (textComponent instanceof Text) {
            Text text = (Text)textComponent;
            text.setText(mTitle);
        }
        Component layout = component.findComponentById(ResourceTable.Id_dependLayout);
        if (layout instanceof DependentLayout) {
            DependentLayout dependentLayout = (DependentLayout) layout;
            ShapeElement element = new ShapeElement();
            element.setRgbColor(RgbColor.fromArgbInt(getFractionAbility().getColor(color)));
            dependentLayout.setBackground(element);
        }
        return component;
    }
}
